<?php
	$ls_gridname	= 'dtl_sample2';


// ----------------- >> Form Detail << ----------------- ?>
<script type="text/javascript">

	function terbilang(bilangan) {
 
		 bilangan    = String(bilangan);
		 var angka   = new Array('0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0');
		 var kata    = new Array('','Satu','Dua','Tiga','Empat','Lima','Enam','Tujuh','Delapan','Sembilan');
		 var tingkat = new Array('','Ribu','Juta','Milyar','Triliun');
		 
		 var panjang_bilangan = bilangan.length;
		 
		 /* pengujian panjang bilangan */
		 if (panjang_bilangan > 15) {
		   kaLimat = "Diluar Batas";
		   return kaLimat;
		 }
		 
		 /* mengambil angka-angka yang ada dalam bilangan, dimasukkan ke dalam array */
		 for (i = 1; i <= panjang_bilangan; i++) {
		   angka[i] = bilangan.substr(-(i),1);
		 }
		 
		 i = 1;
		 j = 0;
		 kaLimat = "";
		 
		 
		 /* mulai proses iterasi terhadap array angka */
		 while (i <= panjang_bilangan) {
		 
		   subkaLimat = "";
		   kata1 = "";
		   kata2 = "";
		   kata3 = "";
		 
		   /* untuk Ratusan */
		   if (angka[i+2] != "0") {
		     if (angka[i+2] == "1") {
		       kata1 = "Seratus";
		     } else {
		       kata1 = kata[angka[i+2]] + " Ratus";
		     }
		   }
		 
		   /* untuk Puluhan atau Belasan */
		   if (angka[i+1] != "0") {
		     if (angka[i+1] == "1") {
		       if (angka[i] == "0") {
		         kata2 = "Sepuluh";
		       } else if (angka[i] == "1") {
		         kata2 = "Sebelas";
		       } else {
		         kata2 = kata[angka[i]] + " Belas";
		       }
		     } else {
		       kata2 = kata[angka[i+1]] + " Puluh";
		     }
		   }
		 
		   /* untuk Satuan */
		   if (angka[i] != "0") {
		     if (angka[i+1] != "1") {
		       kata3 = kata[angka[i]];
		     }
		   }
		 
		   /* pengujian angka apakah tidak nol semua, lalu ditambahkan tingkat */
		   if ((angka[i] != "0") || (angka[i+1] != "0") || (angka[i+2] != "0")) {
		     subkaLimat = kata1+" "+kata2+" "+kata3+" "+tingkat[j]+" ";
		   }
		 
		   /* gabungkan variabe sub kaLimat (untuk Satu blok 3 angka) ke variabel kaLimat */
		   kaLimat = subkaLimat + kaLimat;
		   i = i + 3;
		   j = j + 1;
		 
		 }
		 
		 /* mengganti Satu Ribu jadi Seribu jika diperlukan */
		 if ((angka[5] == "0") && (angka[6] == "0")) {
		   kaLimat = kaLimat.replace("Satu Ribu","Seribu");
		 }
		 
		 return kaLimat + "Rupiah";
	}

	$(document).ready(function(){
		// Setup Form
		$('#form_<?php echo $ls_gridname; ?>').setupForm([
			{ name: 'keterangan_uraian', type: 'text', size: 'long', maxlength: 255 },
			{ name: 'banyak_satuan', type: 'text', size: 'medium', maxlength: 255 },
			{ name: 'harga_satuan', type: 'text', size: 'medium', maxlength: 40},
			{ name: 'total', type: 'text', size: 'medium', maxlength: 40}
		]);	

		$('#harga_satuan').keyup(function() {
			var banyak_satuan =  parseInt($('#banyak_satuan').val());
			var harga_satuan =  parseInt($('#harga_satuan').val());
			var total = harga_satuan * banyak_satuan;
			$('#total').setval(total);
			$('#terbilang').setval(terbilang(total));
		});	


		$('#form_<?php echo $ls_gridname; ?>_container').dialog({
			closed:true,
			modal:true,
			title: 'Tambah Detail',
			width:470,
			height:250,
			buttons:[{
				text:'Save',
				iconCls:'icon-save',
				handler:function(){
					var ls_mode			= $('#form_<?php echo $ls_gridname; ?>').data('mode');
					var ls_mode_detail  = '<?php echo $mode_detail ; ?>' ; 
					var ls_error		= '';
					var row	= formToJSON('form_<?php echo $ls_gridname; ?>');

					// $('#form_master_detail').submitForm('/'+controller+'/ajax_post_detail', { onComplete : function(){ $('#dialog_container_detail').dialog('close'); $('#datagrid_detail').datagrid('reload'); }} );
					
					if(row.kode_kontrak == ''){
						ls_error	+= 'Isi Barang\n';
					}
					if(row.no_invoice == 0){
						ls_error	+= 'Isi Jumlah Barang\n';
					}
					
					// Eksekusi
					if(ls_error!=''){
						$.messager.alert('Error', nl2br(ls_error), 'error');
					}else{
						if(ls_mode=='create'){
							$('#<?php echo $ls_gridname; ?>').datagrid('appendRow', row );
							$('#form_<?php echo $ls_gridname; ?>_container').dialog('close');
						} else {
							var selected = $('#<?php echo $ls_gridname; ?>').datagrid('getSelected');
							var rowIndex = $("#<?php echo $ls_gridname; ?>").datagrid("getRowIndex", selected);

							updateRowFlag('<?php echo $ls_gridname; ?>', row); // Patching Flag Update
							$('#<?php echo $ls_gridname; ?>').datagrid('updateRow',{index:rowIndex,row:row});
							
							$('#form_<?php echo $ls_gridname; ?>_container').dialog('close');
						}
					}
				}
			},{
				text:'Close',
				iconCls:'icon-cancel',
				handler:function(){
					$('#form_<?php echo $ls_gridname; ?>_container').dialog('close');
				}
			}]
		});

	});
</script>


<div id="form_<?php echo $ls_gridname; ?>_container" style="padding:5px;width:800px;height:400px;">
	<div id="form_<?php echo $ls_gridname; ?>">
    
    <table class="tb_form_detail" style="width:100%;" cellpadding="0" cellspacing="0">    
    <tr>
        <td><input id="id_pembayaran_detail" name="id_pembayaran_detail" type="hidden" /></td>
    </tr>
    <tr>
        <td class="label">Keterangan Uraian</td>
        <td><textarea rows="5" id="keterangan_uraian" name="keterangan_uraian" /></td>
    </tr>
	<tr>
        <td class="label">Banyak Satuan</td>
        <td><input id="banyak_satuan" /></td>
    </tr>
    <tr>
        <td class="label">Harga Satuan</td>
        <td><input id="harga_satuan" /></td>
    </tr>
    <tr>
        <td class="label">Total</td>
        <td><input id="total" /></td>
    </tr>
    </table>
	</div>
</div>

<?php 

// ----------------- >> Table Detail << ----------------- ?>
<script type="text/javascript">
	$(document).ready(function(){

		var id_pembayaran_auto = '<?php echo $id_pembayaran_auto ; ?>'
		var id_pembayaran_detail_auto = '<?php echo $id_pembayaran_detail_auto ; ?>'

		$('#<?php echo $ls_gridname; ?>').datagrid({
			url:gs_path+'/'+controller+'/<?php echo $ls_gridname; ?>_data',
			queryParams:{ id_pembayaran:$('#id_pembayaran').getval(),/* rows:9999999999*/ },
			<?php if($mode!='view'){ 
			// -->> Toolbar [Start]
			// Only Appear On Mode Create / Edit ?>
			toolbar:[{
				text:'Add',
				iconCls:'icon-add',
				handler:function(){
					
						resetError();
					
						$('#form_<?php echo $ls_gridname; ?>_container').dialog('open');
						$('#form_<?php echo $ls_gridname; ?>').data('mode', 'create');
						$('#form_<?php echo $ls_gridname; ?>_container').prev().find('.panel-title').html('Tambah Detail Perakitan');					
						$('#form_<?php echo $ls_gridname; ?>').resetForm();
						$('#id_pembayaran').setval(id_pembayaran_auto);
						$('#id_pembayaran_detail').setval(id_pembayaran_detail_auto);

				}
			},'-',{
				text:'Edit',
				iconCls:'icon-edit',
				handler:function(){
					var oSel=$('#<?php echo $ls_gridname; ?>').datagrid('getSelected'); 
					if(oSel==null){ $.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); }
					else{ 
						resetError();
						//alert(oSel['uraian']);
						$('#form_<?php echo $ls_gridname; ?>_container').dialog('open');
						$('#form_<?php echo $ls_gridname; ?>').data('mode', 'edit');
						$('#form_<?php echo $ls_gridname; ?>_container').prev().find('.panel-title').html('Ubah Detail Barang');
						$('#id_pembayaran_detail').setval(oSel['id_pembayaran_detail']);
						jsonToForm(oSel);
					}
				}
			},'-',{
				text:'Delete',
				iconCls:'icon-remove',
				handler:function(){
					var row = $('#<?php echo $ls_gridname; ?>').datagrid('getSelected');
					if(row==null){ $.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); }
					else if(row){
						if(!confirm('Hapus baris data?')) return false;

						var index = $('#<?php echo $ls_gridname; ?>').datagrid('getRowIndex', row);
						$('#<?php echo $ls_gridname; ?>').datagrid('deleteRow', index);
					}
				}
			}],<?php 
			} // -->> Toolbar [End]
			?>
			onClickRow:function(rowIndex){
				var lastIndex = $('#<?php echo $ls_gridname; ?>').data('selectedIndex');
				$('#<?php echo $ls_gridname; ?>').data('selectedIndex', rowIndex);
			},
			onLoadSuccess:function(){ }
		});
		
	});
	
</script>


<table id="<?php echo $ls_gridname; ?>" rownumbers="true" style="width:600;height:auto" singleSelect="true" idField="#" showFooter="true">
<thead>
    <tr>
    	<th field="id_pembayaran_detail" width="200">ID Pembayaran Detail</th>
        <th field="keterangan_uraian" width="200">Keterangan Uraian</th>
    	<th field="banyak_satuan" width="200">Banyak Satuan</th>
    	<th field="harga_satuan" width="200">Harga Satuan</th>
    	<th field="total" width="200">Harga Satuan</th>
    </tr>
</thead>
</table>
